package com.moorecat.app;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.moorecat.MooerApplication;
import com.moorecat.R;
import com.moorecat.bean.Coupon;
import com.moorecat.bean.User;
import com.moorecat.http.HttpUtils;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: yuml
 * Date: 11-7-30
 * Time: 上午12:19
 * 商家优惠券列表适配器
 */
public class BusinessCouponAdapter extends BaseAdapter{
    private BusinessInfo ctx;
    private List<Coupon> list;
    private MooerApplication mooerApp;

    public BusinessCouponAdapter(BusinessInfo ctx, List<Coupon> list, MooerApplication app){
        this.ctx = ctx;
        this.list = list;
        this.mooerApp = app;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final Coupon coupon = list.get(i);
        View itemView = (LinearLayout) LayoutInflater.from(ctx).inflate(
				R.layout.business_coupon_item, null);
        TextView info = (TextView) itemView.findViewById(R.id.youhuiquan_info);
        ImageView download = (ImageView) itemView.findViewById(R.id.download2);
        info.setText(coupon.getContent());
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user = mooerApp.getUser();
                if(!mooerApp.isLoggedin() || user==null || user.getId()==0){
                    Intent intent = new Intent(ctx, MoreAccountSetting.class);
			        intent.putExtra("isFinish", true);
			        ctx.startActivity(intent);
                }else{
                    if(coupon != null && coupon.getId() >0){
                        if(!ctx.isDownloading()){
                            new BusinessCouponSMSTask(ctx, user, coupon).execute("");
                        }
                    }
                }
            }
        });
        return itemView;
    }
}
